<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */


namespace App\Http\Admin\Company\Controllers;

use App\Eloquent\Ygt\Department;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Privilege;
use App\Eloquent\Ygt\Permission;
use App\Eloquent\Ygt\PrivilegePermission;
use App\Eloquent\Ygt\Appindex;
use App\Eloquent\Ygt\PrivilegeAppindex;
use App\Eloquent\Ygt\Appnode;
use App\Eloquent\Ygt\PrivilegeAppnode;
use App\Eloquent\Ygt\PrivilegeDepartment;

class PrivilegeController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        if (request()->isMethod('post')) {

            $where          = ['company_id'=>$company_id];
            $lists          = Privilege::getList( $where );
            return $lists->toJson();
        }
        return $this->view('lists' );
    }
    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $title              = $params['title'];
        $loginStartTime     = $params['login_start_time'];
        $loginEndTime       = $params['login_end_time'];

        $data               = [
            'title'=>$title,'login_start_time'=>$loginStartTime,'login_end_time'=>$loginEndTime,
        ];
        if( $id ){
            $where          = ['company_id'=>$companyId,'id'=>$id];
            $result         = Privilege::updateOneData( $where, $data );
        } else {
            $data['company_id']     = $companyId;
            $result                   = Privilege::insertOneData( $data, 'id' );
        }
        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $id                 = request( 'id', 0 );
        if( $id ){
            $id_arr         = explode(',', $id);
            $where          = ['company_id'=>$company_id,'id'=>['in',$id_arr]];
            $del_result     = Privilege::del($where);
            if( $del_result !== false ){
                $where      = ['privilege_id'=>['in',$id_arr]];
                $del_result = PrivilegePermission::del( $where );
                if( $del_result !== false ){
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败3');
                }
            } else {
                return $this->ajaxFail('失败1');
            }
        } else {
            return $this->ajaxFail('失败2');
        }
    }

    public function permission()
    {
        $id                     = request('id');
        if(request()->isMethod('post')){
            try {
                $id_arr         = request('id_arr');
                if( !empty($id_arr) ){
                    $where      = ['privilege_id'=>$id];
                    PrivilegePermission::del($where);
                    foreach( $id_arr as $key=>$val ){
                        $data           = [
                            'privilege_id'=>$id, 'permission_id'=>$val,
                        ];
                        PrivilegePermission::insertOneData( $data );
                    }
                }
                return $this->ajaxSuccess('成功');
            } catch (\DebugError $e) {
                return $this->ajaxFail($e->getMessage());
            }
        } else {

            $where              = ['id'=>$id];
            $info               = Privilege::getInfo($where);

            $permission_id_arr  = Privilege::getPermissionList($id)->keyBy('id')->keys()->toArray();
            $permission_lists   = Permission::getList('');

            return $this->view('permission', compact('info', 'permission_id_arr', 'permission_lists'));
        }
    }
    public function work()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $id                     = request('id');
        if(request()->isMethod('post')){
            try {
                $id_arr         = request('id_arr');
                if( !empty($id_arr) ){
                    $where      = ['privilege_id'=>$id];
                    PrivilegeAppindex::del($where);
                    foreach( $id_arr as $key=>$val ){
                        $data           = [
                            'privilege_id'=>$id, 'appindex_id'=>$val,
                        ];
                        PrivilegeAppindex::insertOneData( $data );
                    }
                }
                return $this->ajaxSuccess('成功');
            } catch (\DebugError $e) {
                return $this->ajaxFail($e->getMessage());
            }
        } else {

            $where              = ['id'=>$id];
            $info               = Privilege::getInfo($where);
            $appindex_id_arr    = Privilege::getAppindexList($id)->keyBy('id')->keys()->toArray();
            if($companyId == 2){
                $where          = ['is_sale'=>1,'pid'=>3];
            }else{
                $where          = ['is_sale'=>0,'pid'=>3];
            }
            $appindex_lists     = Appindex::getList($where, 'id,title');
//            var_dump($appindex_lists);exit;

            return $this->view('work', compact('info', 'appindex_id_arr', 'appindex_lists'));
        }
    }
    public function node()
    {
        $id                     = request('id');
        if(request()->isMethod('post')){
            try {
                $id_arr         = request('id_arr');
                if( !empty($id_arr) ){
                    $where      = ['privilege_id'=>$id];
                    PrivilegeAppnode::del($where);
                    foreach( $id_arr as $key=>$val ){
                        $data           = [
                            'privilege_id'=>$id, 'appnode_id'=>$val,
                        ];
                        PrivilegeAppnode::insertOneData( $data );
                    }
                }
                return $this->ajaxSuccess('成功');
            } catch (\DebugError $e) {
                return $this->ajaxFail($e->getMessage());
            }
        } else {

            $where              = ['id'=>$id];
            $info               = Privilege::getInfo($where);
            $ids_arr            = Privilege::getAppnodeList($id)->keyBy('id')->keys()->toArray();
            $lists              = Appnode::getList('', 'id,title');
//            var_dump($appindex_lists);exit;

            return $this->view('node', compact('info', 'ids_arr', 'lists'));
        }
    }
    public function contact()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $id                 = request('id');
        if(request()->isMethod('post')){
            $idArr          = request('id_arr');
            $where          = ['privilege_id'=>$id];
            PrivilegeDepartment::del($where);
            $result         = true;
            if($idArr)
            {
                $data           = [];
                foreach( $idArr as $key=>$val ){
                    $data[]     = [
                        'privilege_id'=>$id, 'department_id'=>$val,
                    ];
                }
                $result         = PrivilegeDepartment::insert( $data );
            }
            if( $result ){
                return $this->ajaxSuccess('成功');
            } else {
                return $this->ajaxFail('失败');
            }
        } else {

            $where              = ['id'=>$id];
            $info               = Privilege::getInfo($where)->toArray();
            $where              = ['company_id'=>$companyId];
            $lists              = Department::getList($where)->toArray();
            $idsArr             = Privilege::getDepartmentList($id)->keyBy('id')->keys()->toArray();
            return $this->view('contact', compact('info', 'lists', 'idsArr'));
        }
    }
}